package com.hub.controller.asset;

import com.hub.controller.BaseController;
import com.hub.dto.asset.CategoryInfoDto;
import com.hub.dto.asset.SearchCategoryInfoDto;
import com.hub.utils.ApiResultObject;
import com.hub.utils.PagingResultObject;
import com.hub.service.asset.ICategoryInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author hub
 */
@RestController
@RequestMapping("/category")
public class CategoryInfoController extends BaseController {
    @Autowired
    private ICategoryInfoService categoryInfoService;

    @ApiOperation("分页查询")
    @PostMapping(value = "/list")
    public PagingResultObject getList(@RequestBody @Valid SearchCategoryInfoDto queryModel){
        PagingResultObject ddd = this.categoryInfoService.getList(queryModel);
        return this.categoryInfoService.getList(queryModel);
    }

    @ApiOperation("获取所有分类")
    @GetMapping(value = "/all/list")
    public ApiResultObject getAllList(){
        return this.categoryInfoService.getAllList();
    }

    @ApiOperation("新增分类")
    @PostMapping(value = "/add")
    public ApiResultObject addCategory(@RequestBody @Valid CategoryInfoDto addModel, BindingResult bindingResult) {
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }
        return this.categoryInfoService.addAssetCategory(addModel);
    }

    @ApiOperation("修改分类")
    @PutMapping(value = "/{id}")
    public ApiResultObject modifyCategory(@RequestBody @Valid CategoryInfoDto modifyModel,BindingResult bindingResult) {
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }
        return this.categoryInfoService.modifyAssetCategory(modifyModel);
    }

    @ApiOperation("删除分类")
    @DeleteMapping(value = "/{id}")
    public ApiResultObject deleteCategory(@PathVariable int id){
        return this.categoryInfoService.deleteAssetCategory(id);
    }


    /**
     * 获取所有一级分类信息
     * @return
     */
    @ApiOperation("根据父分类编号，获取所有的子分类")
    @GetMapping(value = "/main")
    public ApiResultObject getAllMainCategory(){
        return categoryInfoService.getAllMainCategory();
    }

    /**
     * 根据父分类编号，获取所有的子分类
     * @param code
     * @return
     */
    @ApiOperation("根据父分类编号，获取所有的子分类")
    @GetMapping(value = "/main/{code}")
    public ApiResultObject getCategoryByParentCode(@PathVariable String code)
    {
        return categoryInfoService.getCategoryByParentCode(code);
    }

}
